package descentFly.app.dialogs.levelsWindow
{
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import descentFly.app.df_block;
	import descentFly.app.df_events;
	import descentFly.app.df_list;
	import descentFly.app.df_modalLayer;
	import descentFly.app.df_window;
	import descentFly.app.radar.df_line;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class df_battlesWindow extends df_window {
		private var _list		: df_list;
		private var _itemClass	: Class;
		private var _event		: Object;
		public function df_battlesWindow(a_parent:df_modalLayer, awidth:int, aheight:int,
											a_title: String, a_items: Object, a_itemClass: Class, a_eventOrFunc: Object,
											canClose:int=1) {
			_itemClass 	= a_itemClass;
			_event		= a_eventOrFunc;
			super(a_parent, awidth, aheight, 'window', true, canClose);
			title.text 	= a_title;
			if ((a_items is Array) && a_items.length) _list.attachList(a_items as Array, doCreateItem);
		}
		
		protected function list(): df_list {
			return _list;
		}
		
		override protected function createDialogComponents(a_layer:df_block):void {
			var inner: Rectangle = a_layer.getInner();
			_list = new df_list(a_layer, inner.x, inner.y, inner.width, inner.height, 'list');
		}
		
		protected function doCreateItem(area: uif_ecomponent, item: Object, i: int): df_levelItem {
			var rect: Rectangle = area.getInner();
			rect.height = df_levelItem.HEIGHT;
			rect.y += i * rect.height;
			return new _itemClass(area, rect.x, rect.y, rect.width, item, _list.style.itemClass);
		}
		
		override protected function createButtons(a_layer: df_block): void {
			addButton(locale.OK, doSelectLevel);
			addButton(locale.CANCEL, doClose);
		}
		
		protected function doSelectLevel(e: Event): void {
			if (_list.selectIndex > -1) {
				close();
				if (_event is Function)
					_event(_list.selectItem._data)
				else app.stage.dispatchEvent(new ObjectEvent(_event as String, _list.selectItem._data));
			}
		}		
	}
}